package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        //**使用builder().属性().属性()...build()的方式创建对象 vo代表返回给前端的数据，EmployeeLoginVO代表返回给前端的实体类，该实体类上必须有@builder注解，这也是Builder设计模式
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     *新增员工数据
     */
    @PostMapping
    @ApiOperation(value = "新增员工数据")
    public Result<String> add(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工数据：{}", employeeDTO);
        employeeService.add(employeeDTO);
        return Result.success();
    }

    /**
     * 分页查询员工数据
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询员工数据")
    public Result<PageResult> list(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("分页查询员工数据：{}", employeePageQueryDTO);
        // 需要返回一个pageResult的对象，该对象中包含了分页查询的结果数据
        PageResult pageResult =  employeeService.queryPage(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id设置账号状态
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "根据id设置账号状态")
    public Result setStatus(@PathVariable Integer status, long id) {
        log.info("根据id设置账号状态：{},{}", status,id);
        employeeService.setStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询员工数据")
    public Result<Employee> showInfo(@PathVariable Long id) {
        log.info("根据id查询员工数据：{}", id);
        Employee employee = employeeService.getUserInfo(id);
        return Result.success(employee);
    }

    /**
     * 更新员工数据
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新员工数据")
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("更新员工数据：{}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
